<?php

/**
 * Project filter form base class.
 *
 * @package    sistema_estadisticas
 * @subpackage filter
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormFilterBaseTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
abstract class BaseFormFilterDoctrine extends sfFormFilterDoctrine
{
	public function setup()
	{
	}

	protected function addTextQuery(Doctrine_Query $query, $field, $values)
	{
		$fieldName = $this->getFieldName($field);

		$this->createTextQuery($query, $fieldName, $values, $query->getRootAlias());
	}

	protected function createTextQuery(Doctrine_Query $query, $fieldName, $values, $alias)
	{
		if (is_array($values))
		{
			if (isset($values['is_empty']) && $values['is_empty'])
			{
				$query->addWhere(sprintf('(%s.%s IS NULL OR %1$s.%2$s = ?)', $alias, $fieldName), array(''));
			}
			elseif (isset($values['text'])  && '' !== $values['text'])
			{
				$keys = explode(' ', $values['text']);

				foreach ($keys as $key)
				{
					$query->addWhere(sprintf('%s.%s LIKE ?', $alias, $fieldName), '%' . $key . '%');
				}
			}
		}
	}

	protected function configureDateWidget($name)
	{
		$this->widgetSchema[$name]->setOption('from_date', new WidgetFormDatePick());
		$this->widgetSchema[$name]->setOption('to_date', new WidgetFormDatePick());
		$this->widgetSchema[$name]->setOption('template', 'De %from_date% a %to_date%');
	}

}
